package com.wf.it.hpwebplan;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.wf.it.common.api.DoctorService;
import com.wf.it.common.api.PlanService;
import com.wf.it.common.entity.Plan;
import com.wf.it.common.util.ForeignHelper;
import com.wf.it.common.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@Api(tags="PlanController-排班信息管理")
public class PlanController {
    @Autowired
    private PlanService planService;
    @Autowired
    private DoctorService doctorService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("根据排班id查询排班对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name="planid",value="排班id",paramType = "query")
    })
    @GetMapping("/plan/findbyid")
    public Result showPlanById(int planid){
       Result<Plan> result=planService.findById(planid);
       if(result.getState()!=200)
           return result;
        ForeignHelper.fillMap(result,doctorService,"findByEmpNo"
                ,"docEmpNo","map","doc");
        return result;
    }

    @ApiOperation("根据医生员工号查询出诊计划列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="empno",value="员工编号",paramType = "query")
    })
    @GetMapping("/plan/findbydoc")
    public Result showPlanByDoc(String empno){
        Result<List<Plan>> planResult= (Result<List<Plan>>) redisTemplate.opsForValue().get("plan_doc_"+empno);
        if(planResult!=null)
            return planResult;
       planResult= planService.findByDocId(empno);
       if(planResult.getState()!=200)
           return planResult;
        ForeignHelper.fillMap(planResult,doctorService,"findByEmpNos"
                ,"docEmpNo","map","doc");
        redisTemplate.opsForValue().set("plan_doc_"+empno,planResult,1, TimeUnit.MINUTES);
        System.out.println("从数据库加载");
        return planResult;

    }
}
